package code;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

public class Student {
    String name;
    int age;

    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        Map<Student,String> smp = new HashMap<>();
        smp.put(new Student("xxs",8), "地址1");
        smp.put(new Student("gzs",15), "地址2");
        smp.put(new Student("dxs",18), "地址3");

        System.out.println("-----------------------");
        // 循环一
        for(Map.Entry<Student, String> entry: smp.entrySet()){
            System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
        }
        System.out.println("-----------------------");
        // 循环二
        for(Student key : smp.keySet()){
            System.out.println("key: " + key + " value: " + smp.get(key));
        }
        System.out.println("-----------------------");
        // 循环三
        Iterator<Map.Entry<Student, String>> it = smp.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<Student, String> en = it.next();
            System.out.println("key: " + en.getKey() + "value: " + en.getValue());
        }
        System.out.println("-----------------------");
        // 循环四
        smp.forEach((key,value)->{
            System.out.println("key: " + key + "value: " + value);
        });
    }
}
